package com.tangkeyon.controller.current;

import com.tangkeyon.common.Const;
import com.tangkeyon.common.ResponseCode;

import com.tangkeyon.pojo.User;
import com.tangkeyon.service.IShoppingCartService;
import com.tangkeyon.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author: tangkeyon@gmail.com
 * @Date: 2019/6/11 11:48
 * @Version 1.0
 */
@Controller
@RequestMapping("/shopping_cart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService iShoppingCartService;

    /**
     * @Description: 添加商品到购物车
     * @param session
     * @param productId
     * @param productCount
     * @return
     */
    @RequestMapping("/add_product_cart.do")
    @ResponseBody
    public ResponseCode<CartVO> addProductToCart(HttpSession session, Integer productId, Integer productCount){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.addProductToCart(user.getId(),productId,productCount);
    }

    /**
     * @Description: 删除购物车中的商品
     * @param session
     * @param productIds 若是批量删除，商品的id拼接成字符串并用下划线分割
     * @return
     */
    @RequestMapping("/delete_product_cart.do")
    @ResponseBody
    public ResponseCode<CartVO> deleteProductFromCart(HttpSession session,String productIds){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.deleteProduct(user.getId(),productIds);
    }

    /**
     * @Description: 修改购物车中商品的数量
     * @param session
     * @param productId
     * @param productCount
     * @return
     */
    @RequestMapping("/update_cart_product_quantity.do")
    @ResponseBody
    public ResponseCode<CartVO> updateCartProductQuantity(HttpSession session, Integer productId, Integer productCount){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.updateCount(user.getId(),productId,productCount);
    }

    /**
     * @Description: 查出当前用户的购物车全部商品
     * @param session
     * @return
     */
    @RequestMapping("/list.do")
    @ResponseBody
    public ResponseCode<CartVO> list(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.list(user.getId());
    }

    /**
     * @Description: 获取当前用户购物车中商品的数量
     * @param session
     * @return
     */
    @RequestMapping("/get_product_quantity.do")
    @ResponseBody
    public ResponseCode<Integer> getProductQuantity(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.getCount(user.getId());
    }

    /**
     * @Description: 选中当前商品，把数据库中这个商品的状态改为选中
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping("/select_product.do")
    @ResponseBody
    public ResponseCode<CartVO> selectProduct(HttpSession session,Integer productId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
         return iShoppingCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.CHECKED);
    }

    /**
     * @Description: 取消选中当前商品，把数据库中这个商品的状态改为未选中
     * @param session
     * @param productId
     * @return
     */
    @RequestMapping("/un_select_product.do")
    @ResponseBody
    public ResponseCode<CartVO> unSelectProduct(HttpSession session,Integer productId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.selectOrUnSelect(user.getId(),productId,Const.Cart.UNCHECKED);
    }


    /**
     * @Description: 将购物车中所有的商品选中
     * @param session
     * @return
     */
    @RequestMapping("/select_all_product.do")
    @ResponseBody
    public ResponseCode<CartVO> selectAllProduct(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.selectOrUnSelect(user.getId(),null,Const.Cart.CHECKED);
    }


    /**
     * @Description: 将购物车中所有的商品取消选中选中
     * @param session
     * @return
     */
    @RequestMapping("/un_select_all_product.do")
    @ResponseBody
    public ResponseCode<CartVO> unSelectAllProduct(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user==null){
            return  ResponseCode.createByErrorMessage("请登录后再操作");
        }
        return iShoppingCartService.selectOrUnSelect(user.getId(),null,Const.Cart.UNCHECKED);
    }

}
